const $ = layui.jquery;
const table=layui.table;
const laydate=layui.laydate;
const form=layui.form;
const layer=layui.layer;
const upload=layui.upload;
$(()=> {
    if(error){
        layer.msg(error);
        return;
    }

    //     渲染图片上传
    upload.render({
        elem: '#logo-upload-btn',
        url: ctx+'/sys/api/v1/upload/image',
        data:{//传的数据
            type:"brand_logo",
        },
        exts:'jpg|png|gif|bmp|jpeg|svg', //上传的类型
        size:1024,
        done: function(resp){//上传完成，响应200状态码进入
            let url =ctx+"/assets/"+resp.data;
            $(".logo-preview").css("background-image","url("+url+")");
            sessionStorage.setItem("logo",resp.data);//将图片地址存到会话存储域
        },
        error: function(){
            layer.msg("上传图片失败");
        },

    });
    // 回显数据,brand是list.js里点击edit后跳转controller后端map放在域里的获,要在edit.html里取出才能用
    form.val("edit-form",brand);

    if(brand.logo){
        let url = ctx + "/assets/"+brand.logo;
        $(".logo-preview").css("background-image","url("+url+")");
        sessionStorage.setItem("logo",brand.logo);
    }


})
// 点击提交按钮后的异步提交操作
function doSubmit(cb) {
    if(error){
        layer.msg(error);
        return;
    }
    let pass = form.validate("#edit-form")//参数验证
    if (!pass) {
        return;
    }
    // 验证通过，向后台发请求
    const url = ctx + "/sys/api/v1/brand";
    let formData = form.val("edit-form");//参数为过滤器
    formData.id = brand.id;
    // 图片填充
    let logoUrl=sessionStorage.getItem("logo");
    if(logoUrl){
        formData.logo=logoUrl;
    }

    $.ajax({
        url,
        method: "put",
        data: formData,
        success(resp) {
            if (resp.success) {
                if (typeof cb === "function") {
                    cb(resp.success)
                }
            }
        }, error(resp) {
            let json = resp.responseJSON;
            let msg = json.msg || "修改会员异常";
            layer.msg(msg);
        }
    })

}